﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PayrollHR.Web;
using Payroll.Common.Utils;
using Payroll.BusinessLogic;
using Payroll.BusinessLogic.Directory;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Utils;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.Devices;
using Payroll.BusinessLogic.Package;

namespace PayrollHR.Administration.Modules
{
    public partial class VoucherInfoControl : BasePayrollAdministrationUserControl
    {
        public int VoucherID
        {
            get
            {
                return CommonHelper.QueryStringInt("VoucherID");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
            }
        }
        public void FillDropDowns(string vendor = null, int? OwnerId = null, int? PackagesID = null)
        {
            this.FillVendorDropDowns(vendor);
            this.FillOwerDropDowns(OwnerId);
            this.FillPacketDropDowns(PackagesID);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void BindVoucher(Voucher item)
        {
            if (CommonHelper.IsNull(item))
                return;
            txtCustName.Text = item.CompanyName;
            txtTaxcode.Text = item.Taxcode;
            txtCustName.Text = item.CompanyName;
            txtAdress.Text = item.Address;
            txtNote.Text = item.Note;
            rblStatus.SelectedIndex = item.Status.Equals("Đang xử lý") ? 1 : 0;
            DatePickerRequied1.SelectedDate = CommonHelper.ToDate(item.Transdate);

        }
        public void FillVendorDropDowns(string vendor = null)
        {
            ddlVendor.Items.Clear();
            ListItem ddlVendorItem0 = new ListItem("----Chọn nhà cung cấp----", "-1");
            ddlVendor.Items.Add(ddlVendorItem0);
            ListItem ddlVendorItem1 = new ListItem("CK", "CK");
            ddlVendor.Items.Add(ddlVendorItem1);
            ListItem ddlVendorItem2 = new ListItem("CK2", "CK2");
            ddlVendor.Items.Add(ddlVendorItem2);
            ListItem ddlVendorItem3 = new ListItem("VINA", "VINA");
            ddlVendor.Items.Add(ddlVendorItem3);
            if (!CommonHelper.IsNull(vendor))
            {
                ddlVendor.SelectedValue = vendor;
            }
        }
        public void FillOwerDropDowns(int? OwnerId = null, int? customerid = null)
        {
            ddlOwner.Items.Clear();
            var owerColection = this.CustomerService.GetAllCustomer();
            owerColection.Insert(0, new Customer { CustomerId  = 0, CustName = "----Chọn đơn vị----" });
            foreach (var customer in owerColection)
            {
                ListItem ddlCustTypeItem2 = new ListItem(customer.CustName, customer.CustomerId.ToString());
                ddlOwner.Items.Add(ddlCustTypeItem2);
                ddlCustomer.Items.Add(ddlCustTypeItem2);
            }
            if (OwnerId.HasValue)
            {
                ddlOwner.SelectedValue = CommonHelper.ToString(OwnerId);
            }
            if (customerid.HasValue)
            {
                ddlCustomer.SelectedValue = CommonHelper.ToString(customerid);
            }
        }
        public void FillPacketDropDowns(int? PackagesID = null)
        {
            ddlPacket.Items.Clear();
            var packetColection = this.PackageService.GetAllPackage();
            packetColection.Insert(0, new Package { PackagesID= 0, Name  = "----Chọn gói cước----" });
            foreach (var packet in packetColection)
            {
                ListItem ddlCustTypeItem2 = new ListItem(packet.Name, packet.PackagesID.ToString());
                ddlPacket.Items.Add(ddlCustTypeItem2);
            }
            if (PackagesID.HasValue)
            {
                ddlPacket.SelectedValue = CommonHelper.ToString(PackagesID);
            }
        }
        public Voucher GetVoucher()
        {
            int UserId = PayrollContext.Current.Session.EmployeeId;
            int? customerid = CommonHelper.ToInteger(ddlCustomer.SelectedItem.Value);
            if (customerid == 0)
                customerid = null;
            int? packetId = CommonHelper.ToInteger(ddlPacket.SelectedItem.Value);
            if (packetId == 0)
                packetId = null;

            int? ownerId = CommonHelper.ToInteger(ddlOwner.SelectedItem.Value);
            if (ownerId == 0)
                ownerId = null;
            string vendor = CommonHelper.ToString(ddlVendor.SelectedItem.Value);
            if (vendor.Equals("-1"))
                vendor = null;
            Voucher voucher = new Voucher
            {
                VoucherID = this.VoucherID,
                Transdate = DatePickerRequied1.SelectedDate.Value.ToString("yyyy-MM-dd"),
                Taxcode = txtTaxcode.Text.Trim(),
                CompanyName = txtCustName.Text.Trim(),
                CustomerId = customerid,
                Address = txtAdress.Text.Trim(),
                PacketId = packetId,
                OwnerId = ownerId,
                Vendor = vendor,
                Status = CommonHelper.ToString(rblStatus.SelectedIndex),
                Note = txtNote.Text.Trim(),
                Createby = UserId,
                Modified = UserId,
                CreateDate = DateTime.Now,
                ModifiedDate = DateTime.Now
            };
            return voucher;
        }
    }
}